package servlet;


import dao.NoteDao;
import dao.RemarkDao;
import entity.Note;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.sql.SQLException;

@WebServlet("*.zan")
public class ServletNotezan extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        PrintWriter printWriter = resp.getWriter();


        //1、设置编码格式
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        //2、拿到请求名
        String requestName = req.getRequestURI().substring(req.getRequestURI().lastIndexOf("/") + 1,
                req.getRequestURI().lastIndexOf("."));
        //3、根据不同的请求进行不同的逻辑派发
        if(requestName.equals("notezanAdd"))
        {
            notezanAdd_service(req,resp);
        }
        else
        {
            notezanSub_service(req,resp);
        }

    }
    protected void notezanAdd_service(HttpServletRequest req, HttpServletResponse resp)
    {

        NoteDao noteDao=new NoteDao();

        int noteid=Integer.parseInt(req.getParameter("noteid"));


        //修改点赞次数让其加一
        try {
            noteDao.updateNotezanAdd(noteid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    protected void notezanSub_service(HttpServletRequest req, HttpServletResponse resp)
    {

        NoteDao noteDao=new NoteDao();

        int noteid=Integer.parseInt(req.getParameter("noteid"));


        //修改点赞次数让其减一
        try {
            noteDao.updateNotezanSub(noteid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
